package com.isunland.managesystem.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.MyFriend;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.widget.MyExpandableTextView;
import java.util.ArrayList;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/3/16 18:55
 */
public class MyFriendListAdapter<T> extends BaseAdapter {

  public static final int EXPERT = 0;//我关注的专家
  public static final int FRIEND = 1;//我的好友
  private ArrayList<T> objects = new ArrayList<>();

  private LayoutInflater layoutInflater;
  private Callback mCallback;
  private int mFrom;
  private SparseBooleanArray collapsedStatus;

  public interface Callback {
    void clickView(int position);
  }

  public MyFriendListAdapter(Context context, ArrayList<T> objects, Callback callback, int from) {
    this.layoutInflater = LayoutInflater.from(context);
    this.objects = objects;
    this.mCallback = callback;
    this.mFrom = from;
    this.collapsedStatus = new SparseBooleanArray();
  }

  @Override public int getCount() {
    return objects.size();
  }

  @Override public T getItem(int position) {
    return objects.get(position);
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @Override public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
      convertView = layoutInflater.inflate(R.layout.adapter_query_count, parent, false);
      convertView.setTag(new ViewHolder(convertView));
    }
    ViewHolder holder = (ViewHolder) convertView.getTag();
    MyFriend item = (MyFriend) getItem(position);

    holder.ivLogo.setImageResource(R.drawable.photo);
    holder.tvBottomRight.setVisibility(View.GONE);

    if (EXPERT == mFrom) {
      holder.tvTitle.setText(item.getToAddName());//专家姓名
      holder.tvContentLeft.setText("专业：");//专家分类
      holder.llExpand.setText(item.getExportKindName(), collapsedStatus,
          position);//专家分类,解决ExpandableTextView被复用

      if (!TextUtils.isEmpty(item.getRemark())) {
        holder.tvBottomLeft.setVisibility(View.VISIBLE);
        holder.tvBottomLeft.setText(MyStringUtil.connect("专家履历：", item.getRemark()));//专家履历
      } else {
        holder.tvBottomLeft.setVisibility(View.GONE);
      }
      holder.tvDataStatus.setBackgroundResource(R.drawable.dianzan_xuanzhong_icon);
      holder.tvDataStatus.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View view) {
          mCallback.clickView(position);
        }
      });
    }
    if (FRIEND == mFrom) {
      holder.tvTitle.setText(item.getToAddNumber());
      //holder.tvContent.setText(item.getToAddName());
      holder.llExpand.setText(item.getToAddName(), collapsedStatus,
          position);//专家,解决ExpandableTextView被复用
      holder.tvBottomLeft.setText(item.getRegDate());
    }
    return convertView;
  }

  static class ViewHolder {
    @BindView(R.id.iv_logo) ImageView ivLogo;
    @BindView(R.id.tv_title) TextView tvTitle;
    @BindView(R.id.tv_contentLeft) TextView tvContentLeft;
    @BindView(R.id.tv_contentRight) TextView tvDataStatus;
    @BindView(R.id.ll_expand) MyExpandableTextView llExpand;
    @BindView(R.id.tv_content2) TextView tvContent2;
    @BindView(R.id.tv_bottomRight) TextView tvBottomRight;
    @BindView(R.id.tv_bottomLeft) TextView tvBottomLeft;

    ViewHolder(View view) {
      ButterKnife.bind(this, view);
    }
  }
}

